package com.malphite.mongodemo.mongo;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Projections;
import com.mongodb.client.model.Sorts;
import org.bson.Document;
import org.bson.conversions.Bson;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Date;


public class Mongo2Find {

    public static void main(String[] args) {
        MongoDatabase test = Mongo1Connection.getDataBase("test");

        //insert(test);
        //eq(test);
        //notEq(test);
        //or(test);
        //in(test);
        //nin(test);
        //sort(test);
        //projection(test);
        Date date = new Date();
        //date.setTime(1665936000000l);
        date.setTime(59960880000000l);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
        String format = simpleDateFormat.format(date);
        System.out.println(format);

        Date date1 = new Date(1970,01,01);
        System.out.println(date1.getTime());

        LocalDate localDate = LocalDate.now();
        String s = localDate.toString();
        System.out.println(s);
    }

    private static void projection(MongoDatabase test) {
        //{ "find" : "places", "projection" : { "_id" : 1, "name" : 1 }
        FindIterable<Document> findIterable = test.getCollection("places").find().
                projection(Projections.include(Arrays.asList("_id","name")));
        MongoCursor<Document> iterator = findIterable.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    private static void sort(MongoDatabase test) {
        //{ "find" : "places", "sort" : { "home" : 1 }
        FindIterable<Document> findIterable = test.getCollection("places").find().sort(Sorts.ascending("home"));
        MongoCursor<Document> iterator = findIterable.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    private static void lt(MongoDatabase test) {
        //{ "find" : "places", "filter" : { "home" : { "$lt" : "a" } }
        Bson name = Filters.lt("home","a");
        FindIterable<Document> findIterable = test.getCollection("places").find(name);
        MongoCursor<Document> iterator = findIterable.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    private static void nin(MongoDatabase test) {
        //{ "find" : "places", "filter" : { "name" : { "$nin" : ["home", "a"] } }
        Bson name = Filters.nin("name", Arrays.asList("home","a"));
        FindIterable<Document> findIterable = test.getCollection("places").find(name);
        MongoCursor<Document> iterator = findIterable.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    private static void in(MongoDatabase test) {
        //{ "find" : "places", "filter" : { "name" : { "$in" : ["home", "a"] } }
        Bson name = Filters.in("name", Arrays.asList("home","a"));
        FindIterable<Document> findIterable = test.getCollection("places").find(name);
        MongoCursor<Document> iterator = findIterable.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    private static void or(MongoDatabase test) {
        Bson name = Filters.or(Filters.eq("name", "home"), Filters.eq("name", "a"));
        FindIterable<Document> findIterable = test.getCollection("places").find(name);
        MongoCursor<Document> iterator = findIterable.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    private static void notEq(MongoDatabase test) {
        //{ "find" : "places", "filter" : { "name" : { "$not" : { "$eq" : null } } }, "$db" : "test", "$readPreference" : { "mode" : "primaryPreferred" } }
        Bson name = Filters.not(Filters.eq("name", null));
        FindIterable<Document> findIterable = test.getCollection("places").find(name);
        MongoCursor<Document> iterator = findIterable.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    private static void eq(MongoDatabase test) {
        //{ "find" : "places", "filter" : { "name" : "home" }, "$db" : "test", "$readPreference" : { "mode" : "primaryPreferred" } }
        Bson name = Filters.eq("name", "home");
        FindIterable<Document> findIterable = test.getCollection("places").find(name);
        MongoCursor<Document> iterator = findIterable.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

    private static void insert(MongoDatabase test) {
        MongoCollection<Document> places = test.getCollection("places");
        Document document = new Document();
        document.append("name","home");
        Document document1 = new Document();
        document1.append("type","Point").append("coordinates", Arrays.asList(-73.9446421, 40.7253944)).append("category","Stadiums");
        document.append("location",document1);
        places.insertOne(document);


        FindIterable<Document> documents = places.find();
        MongoCursor<Document> iterator = documents.iterator();
        while (iterator.hasNext()){
            Document next = iterator.next();
            System.out.println(next.toString());
        }
    }


}
